<?php
namespace Home\Controller;
use Think\Controller;

class SzjxController extends Controller {
    public function index(){
        indexRvent();//测算首页执行事件
        $this->assign('titleName',titleName('Szjx',1));//首页名称
        cookie('orderidnum','Szjx');

        indexRequest($_REQUEST);//测算首页接收数据处理

        //渠道获取统计ID
        $channelID=obtain_channelID('SZ');
        $this->assign('channelID',$channelID);

        $user_agent = $_SERVER['HTTP_USER_AGENT'];//用户使用的浏览器，操作系统等信息。
        if (strpos($user_agent, 'MicroMessenger') == false) {
            //非微信浏览器访问
            cookie('wxlogin',0);
        }else{
            if($_SERVER['HTTP_HOST']=='hy.yixueqm.com'){
                cookie('wxlogin',1);
                if($_REQUEST['openid']!=''){
                    cookie('openid',$_REQUEST['openid'],7100);
                    S('ticket',$_REQUEST['ticket'],7100);
                }
                $openid=cookie('openid');
                if(empty($openid)){
                    openidJump('Bzcy',cookie('channel'));
                }
                cookie('uid',cookie('openid'));
            }else{
                cookie('wxlogin',1);
                getToken();
                getTicket();
                $noncestr=noncestr(15);
                $timestamp=time();
                $ticket= S('ticket');
                $wzurl=wxJsdkData();
                $signature=sha1("jsapi_ticket={$ticket}&noncestr={$noncestr}&timestamp={$timestamp}&url={$wzurl}");

                $this->assign('appid',cookie('APPID'));
                $this->assign('timestamp',$timestamp);
                $this->assign('noncestr',$noncestr);
                $this->assign('signature',$signature);
                wxShareUrl('SZ','Szjx');//分享地址获取

                if(!empty($_REQUEST['code'])){
                    cookie('code',$_REQUEST['code'],7100);
                }
                getCode($wzurl);
                getOopenid();
                cookie('uid',cookie('openid'));
            }
        }

        if(cookie('jploginHC')==''){
            cnzz_pvuv('SZ');//执行统计
            cookie('jploginHC',1,30);
        }


        $this->display("Szjx/ZM_szjx");
    }
    public function szjx_zhifu(){
        $this->assign('titleName',titleName('Szjx',2));//首页名称
        $strData=mb_substr(cookie('orderid'),0,2);
        if($strData=='QS')cookie('orderid',null);
        cookie('timeStrto',time()+7200);//倒计时时间戳
        $user_agent = $_SERVER['HTTP_USER_AGENT'];//用户使用的浏览器，操作系统等信息。
        if (strpos($user_agent, 'MicroMessenger') == false) {
            //非微信浏览器访问
            cookie('wxlogin',0);
        }else{
            cookie('wxlogin',1);
            $this->assign('wxlogin',1);
            if($_SERVER['HTTP_HOST']=='hy.yixueqm.com'){

            }else{
                getToken();
                getTicket();
            }
        }
        cookie('orderidnum','Szjx');

        if($_REQUEST['birthday']){//返回操作
            cookie('zymd',mb_substr($_REQUEST['birthday'],2,10));
            cookie('zhour',mb_substr($_REQUEST['birthday'],13,2));
            cookie('phone',I('request.numberjx'));
            $sztestType=I('request.sztestType');
            cookie('sztestType',$sztestType);
            if($sztestType==1){
                cookie('znickname','手机号');
            }else if($sztestType==2){
                cookie('znickname','车牌号');
            }
        }
        if($_REQUEST['birthday']==''){
            $birthday='0-2018-08-08-8';
            cookie('zymd',$birthday);
        }

        if(!empty($_REQUEST['ordernum'])){//未支付订单
            $arrdata[0]=S('sm_h5test|'.$_REQUEST['ordernum']);
            $zymd=$arrdata[0]['year'].'-'.str_pad($arrdata[0]['month'],2,"0",STR_PAD_LEFT).'-'.str_pad($arrdata[0]['day'],2,'0',STR_PAD_LEFT);
            cookie('zymd',$zymd);
            cookie('zhour',$arrdata[0]['hour']);
            cookie('datetype',$arrdata[0]['datetype']);
            cookie('orderid',$_REQUEST['ordernum']);
            cookie('phone',$arrdata[0]['phone']);
        }

        $y=mb_substr(cookie('zymd'),0,4);//年
        $m=mb_substr(cookie('zymd'),5,2); //月
        $d=mb_substr(cookie('zymd'),8,2);//日
        $zyinli=yinli($y,$m,$d);
        cookie('zyangli',$y.'年'.$m.'月'.$d.'日'.' '.cookie('zhour').'时',604800);//阳历
        cookie('zyinli',$zyinli[0].'年'.$zyinli[1].$zyinli[2],604800);//阴历

        $data=cookie('orderid');//获取订单号
        if(empty($data)&&empty($_REQUEST['ordernum'])){
            $orderid='SZ'.date('Ymd').mt_rand(1000,9999).date('His');
            cookie('orderid',$orderid);
        }

        //创建订单
        $hour=cookie('zhour');if($hour=='')$hour=0;
        $minute=cookie('sztestType');
        $phone=cookie('phone');
        $shop=cookie('orderid');
        $znickname=cookie('znickname');if($znickname=='')$znickname='匿名';
        $channel=cookie('channel');
        $imei=cookie('uid');
        $y=mb_substr(cookie('zymd'),0,4);//年
        $m=mb_substr(cookie('zymd'),5,2); //月
        $d=mb_substr(cookie('zymd'),8,2);//日
        $datetype=cookie('datetype');if($datetype=='')$datetype=0;
        defaultPrice();

        channelPay($channel,'SZ');//查询渠道价格

        if(cookie('couponPrice')){//是否有使用优惠券
            cookie('price',cookie('couponPrice'));
            $this->assign('discount',10);//优惠券价格
        }else{
            $this->assign('discount',0);//优惠券价格
        }

        csPay($znickname,$channel);//测试价格判断

        $price=cookie('price');
        if(empty($_REQUEST['ordernum'])){
//            $arrdata=M()->query("select id from sm_h5test where ordernum='{$shop}'");
//            if(empty($arrdata)){
//                M()->query("insert into sm_h5test (ordernum,price,username,typeid,status,datetype,year,month,day,hour,minute,paykind,phone,appuserid,ip,channel)values(
//                                              '{$shop}','{$price}','{$znickname}',32,-1,{$datetype},{$y},{$m},{$d},{$hour},'{$minute}',-1,'{$phone}','{$imei}','{$_SERVER['REMOTE_ADDR']}','{$channel}')");
//                $IndexController=A('Index');
//                $IndexController->subNumber('SZ');
//            }
//            if(!cookie($_REQUEST['birthday'])){//返回操作
//                //更新订单
//                M()->query("update sm_h5test set username='{$znickname}',datetype='{$datetype}',year={$y},month={$m},day={$d},hour={$hour},minute='{$minute}',phone={$phone} where ordernum='{$shop}'");
//            }
            $arrdata=S('sm_h5test|'.$shop);
            if(empty($arrdata)){
                $smH5testinter=array(
                    'ordernum'=>$shop, 'price'=>$price, 'username'=>$znickname, 'typeid'=>32, 'createtime'=>date('Y-m-d H:i:m'),
                    'status'=>-1, 'datetype'=>$datetype, 'year'=>$y, 'month'=>$m, 'day'=>$d, 'hour'=>$hour,'phone'=>$phone,
                    'appuserid'=>$imei, 'ip'=>$_SERVER['REMOTE_ADDR'], 'channel'=>$channel, 'auto_send_sms'=>0, 'place'=>0,
                );
                S('sm_h5test|'.$shop,$smH5testinter,2592000);//2592000为30天
                cacheZhiFuOrderid($shop);//支付页缓存订单操作

                $IndexController=A('Index');//Index控制器
                $IndexController->subNumber('SZ');//提交订单次数统计
            }else if(!cookie($_REQUEST['username'])){//返回操作
                //更新订单
                $arrdata['username']=$znickname;$arrdata['datetype']=$datetype;
                $arrdata['year']=$y;$arrdata['month']=$m;$arrdata['day']=$d;
                S('sm_h5test|'.$shop,$arrdata,2592000);//2592000为30天
            }

        }

        if(cookie('zsex')==1){$zsex='男';}else{$zsex='女';}
        zwmp(cookie('zymd'), cookie('zhour'),$zsex,'sizhu');
        $sizhu=I('globals.sizhu');
        $this->assign('sizhu',$sizhu);//Z四柱

        if($datetype==1){
            $this->assign('datetype',"农历 ".cookie('zyinli')." ".mb_substr($sizhu[3],1,1,'utf-8')."时");
        }else{
            $this->assign('datetype',"阳历 ".cookie('zyangli'));
        }

        if (strpos($user_agent, 'MicroMessenger') == false) {
            //非微信浏览器访问
            if (strpos($user_agent, 'Windows') == true||strpos($user_agent,'Mac') == true) {
                //Windows 电脑访问
                if(channelSaoma($channel)){
                    $this->assign('pc',1);
                    $saomaUrl=wxsaoma('SZ','数字解析',cookie('orderid'));//微信扫码回调参数
                    $this->assign('wxsaoma',$saomaUrl);
                }
            }
        }else{
            $xml=wxgzhzf('SZ','数字解析',cookie('orderid'));//公众号支付回调参数
            $xmlstring = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
            $val = json_decode(json_encode($xmlstring),true);
            $noncestr=noncestr(15);//随机字符串
            $timestamp=time();//时间戳

            $stringA="appId=".cookie('APPID')."&nonceStr={$noncestr}&package=prepay_id={$val['prepay_id']}&signType=MD5&timeStamp={$timestamp}";
            $stringSignTemp=$stringA."&key=".cookie('wxKEY');
            $sign=strtoupper(md5($stringSignTemp));

            $this->assign('timeStamp',$timestamp);
            $this->assign('nonceStr',$noncestr);
            $this->assign('package','prepay_id='.$val['prepay_id']);
            $this->assign('signType','MD5');
            $this->assign('paySign',$sign);
            $this->assign('appId',cookie('APPID'));
        }

        $this->assign('csNameJump',U('Szjx/jieguoye','',false).'?ordernum='.cookie('orderid'));//支付成功跳转地址

        $this->assign('channel',cookie('channel'));
        $this->assign('znickname',cookie('znickname'));

        $this->display("Szjx/ZM_szjx_zhifu");
    }
    public function jieguoye(){
        jieguoInterface();//结果页公共函数
        $this->assign('titleName',titleName('Szjx',3));//首页名称
        if(empty($_REQUEST['ordernum'])){

        }else{
            $ordernum=$_REQUEST['ordernum'];
            $arrdata[0]=jieguoyeArrData($ordernum);

            $zymd=$arrdata[0]['year'].'-'.str_pad($arrdata[0]['month'],2,"0",STR_PAD_LEFT).'-'.str_pad($arrdata[0]['day'],2,'0',STR_PAD_LEFT);
            cookie('zymd',$zymd);
            cookie('zhour',$arrdata[0]['hour']);
            cookie('phone',$arrdata[0]['phone']);
            cookie('orderid',$arrdata[0]['ordernum']);

            $this->assign("phone",cookie('phone'));
        }

        jieguoyeRequest($arrdata[0]['status']);//结果页公共处理


        $letter=array(1=>'A',2=>'B',3=>'C',4=>'D',5=>'E',6=>'F',7=>'G',8=>'H',9=>'I',10=>'J',
                      11=>'K',12=>'L',13=>'M',14=>'N',15=>'O',16=>'P',17=>'Q',18=>'R',19=>'S',20=>'T',
                      21=>'U',22=>'V',23=>'W',24=>'X',25=>'Y',26=>'Z',);


        $phome=cookie('phone');
        $szjxArr=array();
        if(mb_strlen($phome,'utf-8')>=11){
            for($i=3;$i<=9;$i++){
                $Snumbaer=mb_substr($phome,$i,2);
                if(strlen($Snumbaer)>1 && $Snumbaer!=''){
                    $dataArr=M()->query("select * from tb_szjx_data2 where number like '%{$Snumbaer}%'");
                    if($dataArr[0]['id']){
                        $szjxArr[$i]=$dataArr[0];
                        $szjxArr[$i]['number']=$Snumbaer;
                        $i++;
                    }
                }
            }
            $zwdsNumber[0]=mb_substr($phome,3,2);
            $zwdsNumber[1]=mb_substr($phome,5,2);
            $zwdsNumber[2]=mb_substr($phome,7,2);
            $zwdsNumber[3]=mb_substr($phome,9,2);
            $zwdsNumber[4]=mb_substr($phome,-4,4);
        }else{
            $phome=mb_substr($phome,1,10,'utf-8');
            $this->assign("phone",$phome);
            foreach($letter as $key=>$value){
                $phome=str_replace($value,$key,$phome);
            }
            $this->assign('chepai',$phome);

            for($i=0;$i<=7;$i++){
                $Snumbaer=mb_substr($phome,$i,2);
                if(strlen($Snumbaer)>1 && $Snumbaer!=''){
                    $dataArr=M()->query("select * from tb_szjx_data2 where number like '%{$Snumbaer}%'");
                    if($dataArr[0]['id']){
                        $szjxArr[$i]=$dataArr[0];
                        $szjxArr[$i]['number']=$Snumbaer;
                        $i++;
                    }
                }
            }
            $zwdsNumber[0]=$phome;
            $zwdsNumber[1]=$phome;
            $zwdsNumber[2]=$phome;
            $zwdsNumber[3]=$phome;
            $zwdsNumber[4]=mb_substr($phome,-4,4);
        }
      $LSzhuhe='';
      foreach($szjxArr as $key=>$value){
          $LSzhuhe.="({$value['number']}){$value['name']}、";
      }
        $strData=mb_strlen($LSzhuhe,'utf-8');
        $strData-=1;
        $LSzhuhe=mb_substr($LSzhuhe,0,$strData,'utf-8');

        $this->assign('LSzhuhe',$LSzhuhe);
        $this->assign('zwdsNumber',$zwdsNumber);
        $this->assign('szjxArr',$szjxArr);


        if($zymd!=''){
            zwmp($zymd,cookie('zhour'),'男','zw');
            zsqk(I('globals.srg'),I('globals.arrzx'),I('globals.arrtf'),I('globals.arrjix'),I('globals.arrxiongx'));
            $this->assign('mgarrzhux',I('globals.mgarrzhux')['0']['zengyan']);//自身主星状况

            hyqg(I('globals.srg'),I('globals.arrzx'),I('globals.arrtf'),I('globals.arrjix'),I('globals.arrxiongx'),I('globals.shua'));
            $this->assign('fqarrzhux',I('globals.fqarrzhux')['0']['zengyan']);//婚姻主星状况

            cyzk(I('globals.srg'),I('globals.arrzx'),I('globals.arrtf'),I('globals.arrjix'),I('globals.arrxiongx'),I('globals.shua'));
            $this->assign('cbarrzhux',I('globals.cbarrzhux')['0']['zengyan']);//财运主星状况

            syfz(I('globals.srg'),I('globals.arrzx'),I('globals.arrtf'),I('globals.arrjix'),I('globals.arrxiongx'),I('globals.shua'));
            $this->assign('syarrzhux',I('globals.syarrzhux')['0']['zengyan']);//事业主星状况

            jkzt(I('globals.srg'),I('globals.arrzx'),I('globals.arrtf'),I('globals.arrjix'),I('globals.arrxiongx'),I('globals.shua'));
            $this->assign('jearrzhux',I('globals.jearrzhux')['0']['zengyan']);//疾厄主星状况
        }

        $LYphone=mb_substr(cookie('phone'),-4,4);
        $LYnumx=mb_substr($LYphone,0,2);
        $LYnumy=mb_substr($LYphone,2,2);
        $guaxiang=zhouyiqigua_number($LYnumx,$LYnumy);
        cookie('benguaarr',$guaxiang);
        zhouyiqigua($guaxiang,I('globals.sizhu'));
        $this->assign('arrbengua',I('globals.arrbengua'));//本卦
        $this->assign('arrbiangua',I('globals.arrbiangua'));//变卦
        $this->assign('arrliushou',I('globals.arrliushou'));//六兽
        $this->assign('benguagx',$guaxiang);//本卦卦象
        $this->assign('bianguagx',I('globals.bguaxiang'));//变卦卦象


        //$this->assign('arr',$arrZ);
        $this->display("Szjx/ZM_szjx_zhifu_jieguoye");
    }
    public function pay(){
        $this->assign('ordername1','数字解析');
        $this->assign('ordername2','知命-数字解析');
        $this->display("Index/pay");
    }
    public function paywx(){
        H5ZMwxzhifu('SZ','数字解析',cookie('orderid'),'Szjx');//知命
    }
}